package com.smartrobot.smartrobot.activity.test;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.smartrobot.smartrobot.R;
import com.smartrobot.smartrobot.service.SmartRobotService;
import com.smartrobot.smartrobot.service.SmartRobotService.ServerBinder;


public class testService extends Activity implements OnClickListener{
	
	Button startService,stopService,binderService,unbinderService,showNoti,sendBroadcast;
	ServerBinder binder = null;
	private boolean isBind = false;
	int time;
	
	
	ServiceConnection serviceConn = new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			
		}
		
		//ʹ��ͨѶʱ �᷵�� ibinder�����������԰����������ĳЩ����
		public void onServiceConnected(ComponentName name, IBinder service) {
			//���binder���� �ٿ�service��� ĳ����ʱ������
			binder = (ServerBinder)service;
			binder.startDownload();
			
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.test_service);
		
		init();
		
	}
	
	void init(){
		
		startService = (Button)findViewById(R.id.startServer);
		stopService = (Button)findViewById(R.id.stopServer);
		binderService = (Button)findViewById(R.id.binderServer);
		unbinderService = (Button)findViewById(R.id.unbinderServer);
		showNoti = (Button)findViewById(R.id.showNotification);
		sendBroadcast = (Button)findViewById(R.id.sendBroadcast);
		
		startService.setOnClickListener(this);
		stopService.setOnClickListener(this);
		binderService.setOnClickListener(this);
		unbinderService.setOnClickListener(this);
		showNoti.setOnClickListener(this);
		sendBroadcast.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		
		switch(v.getId()){
		case R.id.startServer://��������
			
			Intent startIntent = new Intent(this,SmartRobotService.class);
			startService(startIntent);
			
			break;
		case R.id.stopServer:
			
			Intent stopIntent = new Intent(this,SmartRobotService.class);
			stopService(stopIntent);
			
			break;
			
		case R.id.binderServer:
			isBind = true;
			Intent binderIntent = new Intent(this,SmartRobotService.class);
			bindService(binderIntent, serviceConn, this.BIND_AUTO_CREATE);
			
			
			break;
			
		case R.id.unbinderServer:
			if(isBind){
				unbindService(serviceConn);
				isBind = false;
			}
			
			break;
			
		case R.id.showNotification:
			time++;
			if(binder != null)
		     binder.showNotification(time);
			else
			  Toast.makeText(getApplicationContext(), "δ�󶨷�����󶨣�", Toast.LENGTH_SHORT).show();
			
			break;
			
		case R.id.sendBroadcast:
			
			if(binder != null)
			     binder.sendBrocastCast();
				else
				  Toast.makeText(getApplicationContext(), "δ�󶨷�����󶨣�", Toast.LENGTH_SHORT).show();
			
			break;
			
		}
		
	}
	
	@Override
	protected void onDestroy() {
		
		startService = null;
		stopService = null;
		binderService = null;
		unbinderService = null;
		
		if(isBind){
			unbindService(serviceConn);
			isBind = false;
		}
		
		super.onDestroy();
	}
	

}
